package org.springside.examples.miniweb.service.user;

import java.util.List;
import java.util.Map;

import org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;
import org.springside.examples.miniweb.dao.user.RoleDAO;
import org.springside.examples.miniweb.dao.user.UserDAO;
import org.springside.examples.miniweb.entity.user.Role;
import org.springside.examples.miniweb.entity.user.User;

/**
 * 负责整个User模块内的业务的Facade.
 * 
 * 事务管理： 使用@Transactional定义所有方法为默认的事务关联策略(REQUIRE)和默认的隔离策略(DEFAULT).
 * 对只读方法，可进一步定义@Transactional(readOnly = true)以加强性能.
 * 在方法如果必须捕获处理异常,需重新抛出继承于RuntimeException的ServiceException触发Rollback.
 * 
 * @author calvin
 */
@Transactional
public class UserManager
{

	private UserDAO userDAO;

	private RoleDAO roleDAO;

	private EhCacheBasedUserCache userCache;

	@Transactional(readOnly = true)
	public User getUser(Integer id)
	{
		return userDAO.findById(id);
	}

	@Transactional(readOnly = true)
	public List<User> getAllUser(int... rowStartIdxAndCount)
	{
		return userDAO.findAll(rowStartIdxAndCount);
	}

	@Transactional(readOnly = true)
	public List<User> getAllByPropertysWithUser(final Map<String, String> propertys, final int... rowStartIdxAndCount)
	{
		return userDAO.findByPropertys(propertys, rowStartIdxAndCount);
	}

	@Transactional(readOnly = true)
	public long getCountUser(final Map<String, String> propertys)
	{
		return (Long) userDAO.findUniqueByPropertys(propertys);
	}

	@Transactional(readOnly = true)
	public User getUserByLoginName(String loginName)
	{
		return userDAO.findByLoginName(loginName);
	}

	public void createUser(User user)
	{
		userDAO.create(user);
	}

	public void updateUser(User user)
	{
		userDAO.update(user);
		// // 如果有注入acegi的userCache，在修改后清空缓存.
		// if (userCache != null)
		// userCache.removeUserFromCache(user.getLoginName());
	}

	public void deleteUser(Integer id)
	{
		User user = userDAO.findById(id);
		userDAO.delete(user);
		// // 如果有注入acegi的userCache，在删除User后清空缓存.
		// if (userCache != null)
		// userCache.removeUserFromCache(user.getLoginName());
	}

	@Transactional(readOnly = true)
	public Role getRole(Integer id)
	{
		return roleDAO.findById(id);
	}

	@Transactional(readOnly = true)
	public List<Role> getAllRole(int... rowStartIdxAndCount)
	{

		return roleDAO.findAll(rowStartIdxAndCount);
	}

	@Transactional(readOnly = true)
	public List<Role> getAllByPropertysWithRole(final Map<String, String> propertys, final int... rowStartIdxAndCount)
	{
		return roleDAO.findByPropertys(propertys, rowStartIdxAndCount);
	}

	@Transactional(readOnly = true)
	public long getCountRole(final Map<String, String> propertys)
	{
		return (Long) roleDAO.findUniqueByPropertys(propertys);
	}

	public void createRole(Role role)
	{
		roleDAO.create(role);
	}

	public void updateRole(Role role)
	{
		roleDAO.update(role);
	}

	public void deleteRole(Integer id)
	{
		roleDAO.delete(id);
	}

	/**
	 * 验证用户名密码.因为acegi已接手安全管理,此函数暂时没有用途.
	 * 
	 * @return 验证通过时返回true.用户名或密码错误均返回false.
	 */
	@Transactional(readOnly = true)
	public boolean auth(String loginName, String password)
	{
		if (StringUtils.isBlank(loginName) || StringUtils.isBlank(password))
			return false;
		return (userDAO.countByAuthInfo(loginName, password) == 1);
	}

	@Required
	public void setUserDAO(UserDAO userDAO)
	{
		this.userDAO = userDAO;
	}

	@Required
	public void setRoleDAO(RoleDAO roleDAO)
	{
		this.roleDAO = roleDAO;
	}

	public void setUserCache(EhCacheBasedUserCache userCache)
	{
		this.userCache = userCache;
	}
}
